package com.roc.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.pojo.TidRecord;
import com.roc.service.IAgentService;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.IProfitShareService;
import com.roc.service.ITerminalService;
import com.roc.service.IorgService;
import com.roc.service.agent.IAgentInfoService;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;


@Component
@Scope("prototype")
@Namespace("/tidRecord")
public class TidRecordAction extends PaginationBaseAction{
	
	private Logger logger=LogManager.getLogger(TidRecordAction.class);
	
	private TidRecordQuerySearchCondition searchCondition;
	
	
	private Operators merchant;
	
	private AuditingItem auditing;
	
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	
	private List<Operators> operatorsList=new ArrayList<Operators>();
	
	private String code;
	
	@Autowired
	IorgService orgService;
	
	@Autowired
	IPagenitionService pagenitionService;
	
	@Autowired 
	IMerchantServices merchantServices;
	
	@Autowired
	IPowerInfoService powerInfoService;
	
	@Autowired
	IAgentInfoService agentInfoService;
	@Autowired
	IMerchantServices merinfoService;
	@Autowired
	IAgentService  agentService;
	
	@Autowired
	IProfitShareService  profitShareService;
	
	@Autowired
	ITerminalService  termService;
	
	private Operators agent;
	
	private TidRecord tidRecord;
	
	
	//终端申请记录初始化
	@Action(value = "tidRecordJsp", results = { @Result(name = "success", location = "/pages/tid_record/tid_record_query.jsp")
             })
	public String queryTidRecordJspAction() throws Exception{
		
		org();
		
		return SUCCESS;
	}
	
	@Action(value = "queryTidRecord", results = { @Result(name = "success", location = "/pages/tid_record/tid_record_query.jsp")
           })
	//查询终端申请记录
	public String queryTidRecordAction() throws Exception{
		
        
		if(searchCondition==null){
			searchCondition=(TidRecordQuerySearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new TidRecordQuerySearchCondition();
			}
		}
		try {
		
			org();	
		
			int rows=pagenitionService.count(searchCondition);
			searchRsList=pagenitionService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());

			setSearchToConditionSession(searchCondition);
			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			addActionError(e.getMessage());
			return ERROR;
		}
		
		return SUCCESS;
	}
	
	
	//查询机构
	public void org() throws Exception{
		Operators logingOperators=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		orgList=orgService.listOrgs(logingOperators);
		getRequest().setAttribute("orgList", orgList);
			
		//加载角色类型 
		Map roleMap=new HashMap();
		if(logingOperators.getRoleType().equals(Role.AGENT.getCode())){
			roleMap.put(Role.AGENT.getCode(),Role.AGENT.getName());
		}else{
			roleMap.put(Role.O_MANAGER.getCode(),Role.O_MANAGER.getName());
			roleMap.put(Role.AGENT.getCode(),Role.AGENT.getName());
		}
		getRequest().setAttribute("roleMap",roleMap);
		
		 Map<String,String> brandMap=ConfigProperties.getPOSBrand();
			Map<String,String> nameMap=ConfigProperties.getPOSName();
			Map<String,String> typeMap=ConfigProperties.getPOSType();
			
			getRequest().setAttribute("brandMap", brandMap);
			getRequest().setAttribute("nameMap", nameMap);
			getRequest().setAttribute("typeMap", typeMap);
			
		
	}
	
	
	//初始化加载页面
	@Action(value = "tidRecordAddJsp", results = {
			@Result(name = "success", location = "/pages/tid_record/tid_record_add.jsp"),
			@Result(name = "error", location = "/error.jsp") }
	)
	public String  tidRecordAddJsp(){
		List orgList=new ArrayList();
		try {
			org();
			Operators logingOperators=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			getRequest().setAttribute("op",logingOperators );
			
			
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(),e);
			addActionError("新增终端记录初始化出错");
		}
		return SUCCESS;
	}
	
	//终端记录录入
	@Action(value = "addTidRecord", results = {
			@Result(name = "success", location = "/pages/tid_record/tid_record_add.jsp"),
			@Result(name = "error", location = "/error.jsp") }
	)
	public String  addTidRecord(){
		Operators logingOperators=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		 
		try {
			
			if(tidRecord==null){
				addActionError("终端数据异常，请重试");
				return ERROR;
			}
			
			
			tidRecord.setOperators(logingOperators.getAccountNo());
			Date date = new Date();
			tidRecord.setDtCreate(date);
			tidRecord.setDtLastmod(date);
			
			termService.saveTidRecord(tidRecord);
			
			org();
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}
		 getRequest().setAttribute("msg", "终端信息录入成功！");
		return SUCCESS;
	}
	
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (TidRecordQuerySearchCondition)getSession().getAttribute(SEARCH_SESSION_TID_RECORD_QUERY);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_TID_RECORD_QUERY, searchCondition);
	}

	public Operators getMerchant() {
		return merchant;
	}

	public void setMerchant(Operators merchant) {
		this.merchant = merchant;
	}

	public TidRecordQuerySearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(TidRecordQuerySearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	

	public AuditingItem getAuditing() {
		return auditing;
	}

	public void setAuditing(AuditingItem auditing) {
		this.auditing = auditing;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}

	public List<Operators> getOperatorsList() {
		return operatorsList;
	}

	public void setOperatorsList(List<Operators> operatorsList) {
		this.operatorsList = operatorsList;
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}


	public Operators getAgent() {
		return agent;
	}

	public void setAgent(Operators agent) {
		this.agent = agent;
	}

	public TidRecord getTidRecord() {
		return tidRecord;
	}

	public void setTidRecord(TidRecord tidRecord) {
		this.tidRecord = tidRecord;
	}    
	
	
	
	
}
